package com.company.eduorder.controller;


import com.company.eduorder.service.PayLogService;
import com.company.utils.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-04-23
 */
@RestController
@RequestMapping("/eduorder/paylog")
@CrossOrigin
public class PayLogController {

    @Resource
    private PayLogService payLogService;

    //生成微信支付二维码接口
    //参数是订单号
    @GetMapping("/createNative/{id}")
    public R createNative(@PathVariable String id) {
        Map map = payLogService.createNative(id);
        System.out.println("*****返回二维码信息：" + map);
        return R.ok().data(map);
    }

    //根据订单ID查询订单状态
    @GetMapping("/queryPayStatus/{id}")
    public R queryPayStatus(@PathVariable String id) {
        //查询订单的支付状态
        Map<String, String> map = payLogService.queryPayStatus(id);
        System.out.println("*****查询订单状态：" + map);
        if (map.get("trade_state").equals("SUCCESS")) {
            //添加支付记录和更新订单状态
            payLogService.updateOrderStatus(map);
            return R.ok().message("支付成功!");
        }
        return R.ok().code(25000).message("支付中...");
    }

}

